package com.ktjy.t382.cheliangtousu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.t382.cheliangtousu.model.Brand;
import com.ktjy.t382.cheliangtousu.model.Vehiclecomplaints;
import com.ktjy.t382.cheliangtousu.repository.BrandMapper;
import com.ktjy.t382.cheliangtousu.repository.VehiclecomplaintsMapper;
import com.ktjy.t382.cheliangtousu.service.VehService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VehServiceImp implements VehService {
    @Autowired
    private VehiclecomplaintsMapper vehiclecomplaintsMapper;
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> allBrands() {
        List<Brand> list = brandMapper.selectList(null);
        return list;
    }

    @Override
    public Vehiclecomplaints selectById(Integer id) {
        Vehiclecomplaints vehiclecomplaints = vehiclecomplaintsMapper.selectById(id);
        return vehiclecomplaints;
    }

    @Override
    public int deleteById(Integer id) {
        int vehiclecomplaints = vehiclecomplaintsMapper.deleteById(id);
        return vehiclecomplaints;
    }

    @Override
    public IPage<Vehiclecomplaints> selectPage(Vehiclecomplaints vehiclecomplaints, Integer pageNo, Integer pageSize) {
        // 创建一个 QueryWrapper 对象，用于构建 MyBatis Plus 的查询条件
        QueryWrapper<Vehiclecomplaints> wrapper = new QueryWrapper<>();
        if (vehiclecomplaints.getBrandName() != null && vehiclecomplaints.getBrandName() != "") {
            wrapper.eq("brandName",vehiclecomplaints.getBrandName());//查询条件
        }
        // 创建一个 Page 对象，指定当前页码和每页显示的记录数
        Page<Vehiclecomplaints> page = new Page<>(pageNo, pageSize);
        //Pager = sysUserMapper.selectPage(page, wrapper);` 调用`sysUser 调用 sysUserMapper 的selectPage 方法，传入分页对象和查询条件，获取分页查询结果
        //        IPage<SysUser> userPagerMapper`的`selectPage`方法，传入分页对象和查询条件，获取分页查询结果。
        IPage<Vehiclecomplaints>  vehiclecomplaintsIPage = vehiclecomplaintsMapper.selectPage(page,wrapper);
        // 处理页码越界问题
        if (pageNo < 1) {
            pageNo = 1;
        }
        return vehiclecomplaintsIPage;
    }

    @Override
    public int updateById(int id,int status) {
        if (status == 1) {
            status = 0;

        }else if (status == 0) {
            status = 1;
        }
        Vehiclecomplaints vehiclecomplaints = vehiclecomplaintsMapper.selectById(id);
        vehiclecomplaints.setStatus(status);
        int count = vehiclecomplaintsMapper.updateById(vehiclecomplaints);
        return count;
    }

    @Override
    public int insert(Vehiclecomplaints vehiclecomplaints) {
        int count = vehiclecomplaintsMapper.insert(vehiclecomplaints);
        return count;
    }
}
